package services

import (
	wms "ez/apps/wms/document"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
)

func UpdateItemStorage(item *wms.ItemSku) {
	rep := wms.NewInLogCrud()
	logs, _ := rep.FindBy(ss.M{
		"itemId":      item.Id,
		"numLeft__ne": 0,
	}, nil, 0, 0)
	var left = 0.0
	var cost = 0.0
	for _, log := range logs {
		log.LoadItem()
		left += log.NumLeft * log.Item.UnitBase / log.Item.UnitExchange
		cost += log.NumLeft * log.BatchPrice
	}
	//检查是否有物料在档没有则建立
	repStore := wms.NewItemMainCrud()
	find, e := repStore.FindOneBy(ss.M{"itemId": item.Id}, nil)
	ez.PrintError(e)
	if find.Id == 0 {
		find.ItemId = item.Id
		//find.Item = item
		find.Num = 0
	}
	find.SkuCode = item.Code
	find.SkuText = item.Text
	find.Num = left
	find.Cost = cost
	find.Save()
}
